$(function (){

    var appPath = $("#appPath").val()

    //点击房型..根据房型..获得空房间号
    $("#taddresident_div_select").click(function (){
        //获取房型的号码
        var houseTypeId = $("#taddresident_div_select").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=roomRates&houseTypeId="+houseTypeId,
            "dataType" : "json",
            "success" : function (result){
                var html = "";
                for (let i = 0; i < result.length; i++) {
                    var roomNumber = result[i].roomNumber
                    html += "<option value='"+roomNumber+"'>"+roomNumber+"</option>"
                }
                $("#addresident_div_select2").html(html)
            },
            "error" : function (){
                alert("错")
            }
        })
    })

    //点击房型..根据房型..获得房型的价格
    $("#taddresident_div_select").click(function (){
        //获取房型号码
        var typeId = $("#taddresident_div_select").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=roomPrice&houseTypeId="+typeId,
            "dataType" : "json",
            "success" : function (result){

                if (typeId <= 0){
                    $("#add_price").val("0.00")
                }else {
                    $("#taddresident_div_select").removeClass()
                    $("#add_price").val(result.price)
                    //时间失焦
                    $("#add_checkOutDate").blur(function (){
                        //开房时间
                        let inDat = $("#add_checkInDate").val()
                        //退房时间
                        let outDate = $("#add_checkOutDate").val()
                        //计算毫秒
                        let second = parseInt(new Date(outDate).getTime() / 1000) - parseInt(new Date(inDat).getTime() / 1000)
                        //计算开房天数
                        let day = parseInt(second / 60 / 60 / 24);
                        //计算小时
                        let hour = parseInt(second / 60 / 60)
                        //是否满足一天
                        if (hour % 24 == 0){
                            day = day;
                        }else if (hour % 24 != 0){
                            day = day +1
                        }
                        //计算价格*开房天数
                        let price = result.price * day;
                        $("#add_price").val(price)
                    })
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    }).blur(function (){
        //获取手机号码
        var phone = $("#add_mobilePhone").val()
        //获取金额
        var price = $("#add_price").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=vip&mobilePhone="+phone,
            "dataType" : "json",
            "success" : function (result){
                //获取折扣
                var discount = result.discount
                //判断是不是会员
                if (!isNaN(result)){
                    price = price * discount
                    $("#add_price").val(price)
                }else {
                    price = price
                    $("#add_price").val(price)
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    })

    //电话号码失焦，验证是否是会员
    $("#add_mobilePhone").blur(function (){
        //获取电话号码
        var phone = $("#add_mobilePhone").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=vip&mobilePhone="+phone,
            "dataType" : "json",
            "success" : function (result){
                //获取折扣
                var discount = result.discount
                //获取金额
                var price = $("#add_price").val()
                //判断是否是会员
                if (!isNaN(result) || result != ""){
                    price = price * discount
                    let money = parseFloat(price).toFixed(2)
                    $("#add_price").val(money)
                }else {
                    price = price
                    let money = parseFloat(price).toFixed(2)
                    $("#add_price").val(money)
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    })
})
